<?php
namespace app\admin\controller\category;

use app\admin\controller\Controller;
use think\facade\Db;

class Category extends Controller
{
    /**
     * 获取分类列表
     */
    public function list()
    {
        try {
            // 获取请求参数
            $page = input('page/d', 1);
            $limit = input('limit/d', 10);
            $name = input('name/s', '');
            
            // 构建查询条件
            $where = [];
            if (!empty($name)) {
                $where[] = ['name', 'like', '%' . $name . '%'];
            }
            
            // 查询数据
            $total = Db::name('science_category')
                ->where($where)
                ->count();
            
            $list = Db::name('science_category')
                ->where($where)
                ->page($page, $limit)
                ->order('id desc')
                ->select();
            
            // 格式化时间
            foreach ($list as &$item) {
                if (!empty($item['create_time'])) {
                    $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
                }
                if (!empty($item['update_time'])) {
                    $item['update_time'] = date('Y-m-d H:i:s', $item['update_time']);
                }
            }
            
            return json([
                'error' => 0,
                'msg' => 'success',
                'data' => [
                    'lists' => $list,
                    'total' => $total,
                    'page' => $page,
                    'limit' => $limit
                ]
            ]);
        } catch (\Exception $e) {
            return json([
                'error' => 1,
                'msg' => $e->getMessage(),
                'data' => []
            ]);
        }
    }
    
    /**
     * 获取分类详情
     */
    public function detail()
    {
        try {
            $id = input('id/d', 0);
            
            if (empty($id)) {
                return json(['error' => 1, 'msg' => 'ID不能为空']);
            }
            
            $info = Db::name('science_category')
                ->where('id', $id)
                ->find();
            
            if (empty($info)) {
                return json(['error' => 1, 'msg' => '分类不存在']);
            }
            
            // 格式化时间
            if (!empty($info['create_time'])) {
                $info['create_time'] = date('Y-m-d H:i:s', $info['create_time']);
            }
            if (!empty($info['update_time'])) {
                $info['update_time'] = date('Y-m-d H:i:s', $info['update_time']);
            }
            
            return json(['error' => 0, 'msg' => 'success', 'data' => $info]);
        } catch (\Exception $e) {
            return json(['error' => 1, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 创建分类
     */
    public function create()
    {
        try {
            $data = [
                'name' => input('name/s', ''),
                'create_time' => time(),
                'update_time' => time()
            ];
            
            if (empty($data['name'])) {
                return json(['error' => 1, 'msg' => '分类名称不能为空']);
            }
            if (mb_strlen($data['name']) > 50) {
                return json(['error' => 1, 'msg' => '分类名称不得超过50个字符']);
            }
            $data['name'] = htmlspecialchars(trim($data['name']), ENT_QUOTES);
            
            $id = Db::name('science_category')->insertGetId($data);
            
            return json(['error' => 0, 'msg' => '创建成功', 'data' => ['id' => $id]]);
        } catch (\Exception $e) {
            return json(['error' => 1, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 更新分类
     */
    public function update()
    {
        try {
            $id = input('id/d', 0);
            $name = input('name/s', '');
            
            if (empty($id)) {
                return json(['error' => 1, 'msg' => 'ID不能为空']);
            }
            
            if (empty($name)) {
                return json(['error' => 1, 'msg' => '分类名称不能为空']);
            }
            if (mb_strlen($name) > 50) {
                return json(['error' => 1, 'msg' => '分类名称不得超过50个字符']);
            }
            $name = htmlspecialchars(trim($name), ENT_QUOTES);
            
            $data = [
                'name' => $name,
                'update_time' => time()
            ];
            
            Db::name('science_category')
                ->where('id', $id)
                ->update($data);
            
            return json(['error' => 0, 'msg' => '更新成功']);
        } catch (\Exception $e) {
            return json(['error' => 1, 'msg' => $e->getMessage()]);
        }
    }
    
    /**
     * 删除分类
     */
    public function delete()
    {
        try {
            $id = input('id/d', 0);
            
            if (empty($id)) {
                return json(['error' => 1, 'msg' => 'ID不能为空']);
            }
            
            Db::name('science_category')
                ->where('id', $id)
                ->delete();
            
            return json(['error' => 0, 'msg' => '删除成功']);
        } catch (\Exception $e) {
            return json(['error' => 1, 'msg' => $e->getMessage()]);
        }
    }
}